var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var xmlparser = require('express-xml-bodyparser');

var globalVar = require('./global_var');

var index = require('./routes/index');
var users = require('./routes/users');
var admin = require('./routes/admin');
var admin_management = require('./routes/admin_management');
var app_management = require('./routes/app_management');
var vip_management = require('./routes/vip_management');
var agent = require('./routes/agent');
var app_mng_pri = require('./routes/app_mng_pri');
var admin_user = require('./routes/admin_user');
var log = require('./routes/log');
var payment = require('./routes/payment');
var userCash = require('./routes/user_cash');
var srvLink = require('./routes/srv_link');
var userMoney = require('./routes/user_money');
var recommender = require('./routes/recommender');
var userAgent = require('./routes/user_agent');
var freezeMoney = require('./routes/freeze_money');
var config = require('./routes/config');
var sign = require('./routes/sign');
var simuSocket = require('./routes/simu_socket');
var appkey= require('./routes/appkey');
var alipayInfo = require('./routes/alipayInfo');
var tao_acc_collection = require('./routes/tao_acc_collection');
var user_sec = require('./routes/user_sec');
var short_url = require('./routes/short_url');

var app = express();
app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods","POST,GET");
    res.header("X-Powered-By",' 3.2.1');
    next();
});

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(xmlparser());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());


app.use('/acc/', index);
app.use('/users', users);
app.use('/admin',admin);
app.use('/admin_management',admin_management);
app.use('/app_management',app_management);
app.use('/vip_management',vip_management);
app.use('/agent',agent);
app.use('/app_mng_pri',app_mng_pri);
app.use('/admin_user',admin_user);
app.use('/log',log);
app.use('/payment',payment);
app.use('/userCash',userCash);
app.use('/srvLink',srvLink);
app.use('/userMoney',userMoney);
app.use('/recommender',recommender);
app.use('/userAgent',userAgent);
app.use('/freeze_money',freezeMoney);
app.use('/config',config);
app.use('/sign', sign);
app.use('/im',simuSocket);
app.use('/appkey', appkey);
app.use('/alipay_info',alipayInfo);
app.use('/tao_acc_collection',tao_acc_collection)
app.use('/user_sec',user_sec);
app.use('/sl',short_url);

app.use(express.static(path.join(__dirname, 'public/benliu_web/')));
app.use(express.static(path.join(__dirname, 'public')));


// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

simuSocket.start();

module.exports = app;
